package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
@RestController
public class RoleHandler {

    @Autowired
    private RoleService roleService;

//    @ResponseBody
    @RequestMapping("/role/do/remove/by/role/id/array.json")
    public ResultEntity<String> removeRole(@RequestBody List<Integer> roleIdList){

        roleService.removeRole(roleIdList);

        return ResultEntity.successWithoutData();
    }

//    @ResponseBody
    @RequestMapping("/role/do/update.json")
    public ResultEntity<String> updateRole(Role role){

        roleService.updateRole(role);

        return ResultEntity.successWithoutData();
    }

//    @ResponseBody
    @RequestMapping("/role/do/save.json")
    public ResultEntity<String> roleSave(Role role){

        roleService.save(role);

        return ResultEntity.successWithoutData();
    }

//    @ResponseBody
    @RequestMapping("/role/get/page/Info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){

        // 调用Service方法获取pageInfo对象
        PageInfo<Role> pageInfo = roleService.getPageInfo(keyword, pageNum, pageSize);

        // 封装到ResultEntity对象中返回（如果上面的操作抛异常，交给异常处理机制处理）
        return ResultEntity.successWithData(pageInfo);
    }



}
